package com.polaris.live.popup

import androidx.activity.viewModels
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.GridLayoutManager
import com.lxj.xpopup.core.DrawerPopupView
import com.polaris.live.R
import com.polaris.live.adapter.LiveDrawerAdapter
import com.polaris.live.databinding.PopupLiveDrawerBinding
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.viewmodel.live.LiveDrawerViewModel

/**
 * LiveDrawerPopup
 *
 * @author Created by 半仙 on 2023/10/9/009 21:12
 */
class LiveDrawerPopup(
    context: FragmentActivity,
    private val onToLive: (DisplayUserBean) -> Unit
) : DrawerPopupView(context) {

    private lateinit var mBinding: PopupLiveDrawerBinding

    override fun getImplLayoutId(): Int {
        return R.layout.popup_live_drawer
    }

    private val mViewMode by context.viewModels<LiveDrawerViewModel>()
    private val mAdapter by lazy { LiveDrawerAdapter(true) }

    override fun onCreate() {
        super.onCreate()

        mBinding = PopupLiveDrawerBinding.bind(popupImplView)

        mBinding.refresh.apply {
            recycler.layoutManager =
                GridLayoutManager(context, 2, GridLayoutManager.VERTICAL, false)
            attachedAdapter(mAdapter, mViewMode).observe(
                this@LiveDrawerPopup,
                mViewMode.livesLiveData
            )
        }

        mAdapter.setOnItemClickListener { _, _, position ->
            val item = mAdapter.getItem(position)

            onToLive.invoke(item)
        }
    }

    override fun onShow() {
        super.onShow()

        mBinding.refresh.autoRefresh()
    }
}